package Stack;

import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;
    public MyStack(){
        elem = new int[DEFAULT_SIZE];
    }

    public void push(int val){
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }

        elem[usedSize] = val;
        usedSize++;
    }

    private boolean isFull(){
        return usedSize==elem.length;
    }

    public int pop(){
        if(isEmpty()){
            return -1;
        }
        int data = elem[usedSize-1];
        usedSize--;
        return data;
    }

    private boolean isEmpty(){
        return usedSize == 0;
    }

    public int peek(){
        if(isEmpty()){
            return -1;
        }
        int data = elem[usedSize-1];
        return data;
    }

    public int size(){
        return usedSize;
    }

}
class test {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(12);
        myStack.push(23);
        myStack.push(34);
        myStack.push(45);
        System.out.println(myStack.pop());
        System.out.println(myStack.peek());
        System.out.println(myStack.peek());
    }
}
